package com.team.admin.config;

import com.team.admin.auth.*;
import com.team.admin.service.CustomerUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomerUserDetailService customerUserDetailService;

    @Autowired
    private IgnoreUrlProperties ignoreUrlProperties;

    @Autowired
    private CustomerAuthenticationEntryPoint customerAuthenticationEntryPoint;

    @Autowired
    private CustomerAccessDeniedHandler customerAccessDeniedHandler;

    @Autowired
    private CustomerJwtAuthenticationFilter customerJwtAuthenticationFilter;

    @Autowired
    private CustomerAuthResponseHandler customerAuthResponseHandler;

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customerUserDetailService)
                .passwordEncoder(bCryptPasswordEncoder());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers(ignoreUrlProperties.getUrls()).permitAll()
                .anyRequest().authenticated()
                .and().exceptionHandling()
                .authenticationEntryPoint(customerAuthenticationEntryPoint)
                .accessDeniedHandler(customerAccessDeniedHandler)
                .and().sessionManagement()
                    .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and().cors()
                .and().csrf().disable();

        CustomerLoginAuthenticationFilter customerLoginAuthenticationFilter =
                new CustomerLoginAuthenticationFilter(authenticationManager(),customerAuthResponseHandler);
        http.addFilterBefore(customerLoginAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        http.addFilterAfter(customerJwtAuthenticationFilter,CustomerLoginAuthenticationFilter.class);
    }

}
